package com.goldcn.user.controller;


import com.goldcn.util.CommonTools;
import com.goldcn.util.SystemProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pengzh on 2017/5/2.
 */

@RestController
@RequestMapping(value = "/utilsCon")
public class UtilsController {
    @Autowired
    RestTemplate restTemplate;


    /***
     * 获取当前版本号
     * @param systemName
     * @return
     */
    @RequestMapping(value = "appVersion/{systemName}", method = RequestMethod.GET)
    public Object userInfo(@PathVariable String systemName) throws UnsupportedEncodingException {
        Map data = new HashMap();
        String version = "";
        String url = "";
        String name = systemName;
        Map<String,String> ext_app = CommonTools.getExtForApp(restTemplate);
        if (StringUtils.isNotEmpty(name) && name.equals("apk")) {
            version = ext_app.get( "app_version.apk");
            url = ext_app.get("app_version.apk.url");
            data.put("versionNO", ext_app.get( "app_version.apk.versionNO"));
            data.put("updateDate", ext_app.get("app_version.apk.updateDate"));
            data.put("versionName", ext_app.get("app_version.apk.versionName"));
            String s = ext_app.get("app_version.apk.versionDesc");
            String s1 = new String(s.getBytes(ext_app.get("app_version.apk.code")), ext_app.get("app_version.apk.code")).trim();
            //String code = getEncoding(s);
            /*    String  versionDesc= new String(SystemProperties.getProperties().getProperty("app_version.apk.versionDesc").getBytes( "UTF-8"));
                System.out.println(versionDesc);
                String unicode = new String(versionDesc.getBytes(),"UTF-8");
                System.out.println(unicode);
                String gbkversionDesc = new String(unicode.getBytes("GBK"));*/

            data.put("versionDesc", s1);
            data.put("packageSize", ext_app.get( "app_version.apk.packageSize"));
            data.put("isForce", ext_app.get("app_version.apk.isForce"));
            data.put("link", ext_app.get("app_version.apk.link"));
            data.put("other", ext_app.get("app_version.apk.other"));
            data.put("version", version);
            data.put("url", url + "");

        } else if (StringUtils.isNotEmpty(name) && name.equals("ipa")) {
            data.put("versionNO", ext_app.get("app_version.ipa.versionNO"));
            data.put("updateDate", ext_app.get("app_version.ipa.updateDate"));
            data.put("versionName", ext_app.get("app_version.ipa.versionName"));
            String s = ext_app.get("app_version.ipa.versionDesc");
            String s1 = new String(s.getBytes( ext_app.get("app_version.ipa.code")),  ext_app.get("app_version.ipa.code")).trim();
            data.put("versionDesc", s1);
            data.put("packageSize", ext_app.get("app_version.ipa.packageSize"));
            data.put("isForce", ext_app.get("app_version.ipa.isForce"));
        }

        return data;
    }

    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }

    /***
     * 获取当前版本号App统计报表
     * @param systemName
     * @return
     */
    @RequestMapping(value = "appStaticVersion/{systemName}", method = RequestMethod.GET)
    public Object appStaticVersion(@PathVariable String systemName) throws UnsupportedEncodingException {
        Map data = new HashMap();
        String version = "";
        String url = "";
        String name = systemName;
        Map<String,String> ext_app = CommonTools.getExtForApp(restTemplate);
        if (StringUtils.isNotEmpty(name) && name.equals("apk")) {
            version = ext_app.get("app_rpt_version.apk");
            url = ext_app.get("app_rpt_version.apk.url");
            data.put("versionNO", ext_app.get("app_rpt_version.apk.versionNO"));
            data.put("updateDate", ext_app.get("app_rpt_version.apk.updateDate"));
            data.put("versionName", ext_app.get("app_rpt_version.apk.versionName"));
            String s = ext_app.get("app_rpt_version.apk.versionDesc");
            String s1 = new String(s.getBytes( ext_app.get("app_version.apk.code")),  ext_app.get("app_version.apk.code")).trim();
            /*    String  versionDesc= new String(SystemProperties.getProperties().getProperty("app_version.apk.versionDesc").getBytes( "UTF-8"));
                System.out.println(versionDesc);
                String unicode = new String(versionDesc.getBytes(),"UTF-8");
                System.out.println(unicode);
                String gbkversionDesc = new String(unicode.getBytes("GBK"));*/

            data.put("versionDesc", s1);
            data.put("packageSize", ext_app.get("app_rpt_version.apk.packageSize"));
            data.put("isForce", ext_app.get("app_rpt_version.apk.isForce"));
            data.put("link", ext_app.get("app_rpt_version.apk.link"));
            data.put("other", ext_app.get("app_rpt_version.apk.other"));
            data.put("version", version);
            data.put("url", url + "");

        } else if (StringUtils.isNotEmpty(name) && name.equals("ipa")) {
            data.put("versionNO", ext_app.get("app_rpt_version.ipa.versionNO"));
            data.put("updateDate", ext_app.get( "app_rpt_version.ipa.updateDate"));
            data.put("versionName", ext_app.get("app_rpt_version.ipa.versionName"));
            String s = ext_app.get("app_rpt_version.ipa.versionDesc");
            String s1 = new String(s.getBytes( ext_app.get("app_version.ipa.code")), ext_app.get("app_version.ipa.code")).trim();
            data.put("versionDesc", s1);
            data.put("packageSize", ext_app.get("app_rpt_version.ipa.packageSize"));
            data.put("isForce", ext_app.get("app_rpt_version.ipa.isForce"));
        }

        return data;
    }
}

